package com.snail.billing.page;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.BoundMoblieChangeSession;
import com.snail.billing.session.BoundMoblieChangeSession2;

public class BoundMoblieChangePage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private TextView textView;
	private Button button;

	private int state;

	private Account account;
	private HttpApp httpApp;
	private BoundMoblieChangeSession boundMoblieChangeSession;
	private BoundMoblieChangeSession2 boundMoblieChangeSession2;
	private BoundMoblieChangeSession.JsonData jsonData;
	private BoundMoblieChangeSession2.JsonData jsonData2;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_bound_mobile_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_bound_mobile_title2));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		textView = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bound_mobile_text));
		button = (Button) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bound_mobile_button));
		button.setOnClickListener(this);
		//
		httpApp = new HttpApp(getContext());
		httpApp.setOnHttpResultListener(this);
		//
		account = AccountManager.getCurrentAccount();
	}

	@Override
	public void onResume() {
		super.onResume();
		if (state == 0) {
			if (jsonData == null) {
				boundMoblieChangeSession = new BoundMoblieChangeSession(account.getAccount());
				httpApp.request(boundMoblieChangeSession);
			} else {
				showText();
			}
		}
	}

	private void showText() {
		state = 0;
		String html = getHtml(jsonData.getContext(), jsonData.getMobile());
		textView.setText(Html.fromHtml(html));
		button.setText(ResUtil.getString(Resource.string.snailbilling_bound_mobile_button1));
	}

	private void showText2() {
		state = 1;
		String html = getHtml(jsonData2.getContext(), jsonData2.getMobile());
		textView.setText(Html.fromHtml(html));
		button.setText(ResUtil.getString(Resource.string.snailbilling_bound_mobile_button2));
	}

	private String getHtml(String content, String moblie) {
		StringBuilder builder = new StringBuilder();
		builder.append("<span>\t\t");
		if (state == 0) {
			builder.append(ResUtil.getString(Resource.string.snailbilling_bound_mobile_content));
			builder.append(getPageArgs().getString("text"));
		} else if (state == 1) {
			builder.append(ResUtil.getString(Resource.string.snailbilling_bound_mobile_content_new));
		}
		builder.append(ResUtil.getString(Resource.string.snailbilling_bound_mobile_content2));
		builder.append("<font color=\"#ff2e36\">" + content + "</font>");
		builder.append(ResUtil.getString(Resource.string.snailbilling_bound_mobile_content3));
		builder.append("<font color=\"#ff2e36\">" + showMobile(moblie) + "</font>");
		builder.append(ResUtil.getString(Resource.string.snailbilling_bound_mobile_content4));
		builder.append("</span>");
		return builder.toString();
	}

	private String showMobile(String mobile) {
		if (TextUtils.isEmpty(mobile)) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		char[] chars = mobile.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i < 3 || i > chars.length - 4) {
				builder.append(chars[i]);
			} else {
				builder.append("*");
			}
		}
		return builder.toString();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			if (state == 1) {
				showText();
				return;
			}
			getPageManager().backward();
		} else if (v.equals(button)) {
			boundMoblieChangeSession2 = new BoundMoblieChangeSession2(account.getAccount());
			httpApp.request(boundMoblieChangeSession2);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(boundMoblieChangeSession)) {
				jsonData = new BoundMoblieChangeSession.JsonData((String) httpSession.getResponseData());
				if (jsonData.getCode() == 1) {
					showText();
				} else {
					Toast.makeText(getContext(), jsonData.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(boundMoblieChangeSession2)) {
				jsonData2 = new BoundMoblieChangeSession2.JsonData((String) httpSession.getResponseData());
				if (jsonData2.getCode() == 1) {
					showText2();
				} else {
					Toast.makeText(getContext(), jsonData2.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}

	}

}
